﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.Xna.Framework.Input;
using cocos2d;
using CocosDenshion;

namespace RunCircle
{
    class LogoUI : CCLayer
    {
        public CCSprite pLogo;
        public CCSprite pClickToStart;
        public ProgressBar pLogoProgressBar;
        private Thread thread;
        bool bFinishLoad;
        //
        protected MenuNode PVP_Btn;
        protected MenuNode PVC_Btn;

        //
        CCSize winsize;

        public override bool init()
        {
            bFinishLoad = false;
            //
//             CCDirector.sharedDirector().deviceOrientation =
//                 ccDeviceOrientation.CCDeviceOrientationPortrait;
            //
            winsize = CCDirector.sharedDirector().getWinSize();
            pLogo = CCSprite.spriteWithFile(@"images/logo");
            pLogo.position = new CCPoint(winsize.width / 2, winsize.height / 2);
            addChild(pLogo, 0);

            //
            pLogoProgressBar = new ProgressBar(100, 0);
            addChild(pLogoProgressBar, 1);
            pLogoProgressBar.position = new CCPoint((winsize.width - pLogoProgressBar.width) / 2,
                                                     winsize.height  - 170 );
            //
            pClickToStart = null;
            PVP_Btn = null;
            PVC_Btn = null;

            pLogoProgressBar.visible = true;

            SimpleAudioEngine.sharedEngine().playBackgroundMusic(ResDef.g_OpeningMusic, true);

            //
            return base.init();   
        }
        //
        public static CCScene scene()
        {
            CCScene scene = CCScene.node();

            CCLayer layer = LogoUI.node();

            // add layer as a child to scene
            scene.addChild(layer);

            // return the scene
            return scene;
        }
        //
        public static new CCLayer node()
        {
            LogoUI ret = new LogoUI();
            if (ret.init())
            {
                return ret;
            }
            else
            {
                ret = null;
            }

            return ret;
        }
        //////////////////////////////////////////////////////////////////////////
        public override void onEnter()
        {

            this.schedule(new SEL_SCHEDULE(this.update));
            //
            if (bFinishLoad == false)
            {
                   thread = new Thread(new ThreadStart(loadRes));
                   thread.Start();
            }


            base.onEnter();
        }
        private void loadRes()
        {

            ResDef.initRes(out pLogoProgressBar.CurrentProgress);
            pLogoProgressBar.visible = false;
            //
            if (PVP_Btn == null)
            {
                PVP_Btn = new MenuNode("pvp_1.png", "pvp_2.png", new SEL_MenuHandler(StartPVP_Game));
                PVP_Btn.position = new CCPoint(winsize.width / 2, 480 - 216);
                this.addChild(PVP_Btn,3);
                //
                PVP_Btn.visible = true;
                //
            }
            //
            if (PVC_Btn == null)
            {
                PVC_Btn = new MenuNode("pvc_1.png", "pvc_2.png", new SEL_MenuHandler(StartPVC_Game));
                PVC_Btn.position = new CCPoint(winsize.width / 2 + 10, 480 - 256);                
                this.addChild(PVC_Btn, 3);
                //
                PVC_Btn.visible = true;
                //
            }
            //
            bFinishLoad = true;
        }
        public override void update(float dt)
        {
            if (bFinishLoad)
            {
                if (PVP_Btn != null)
                {
                    PVP_Btn.update(dt);
                }
                //
                if (PVC_Btn != null)
                {
                    PVC_Btn.update(dt);
                }

            }
            else
            {
                pLogoProgressBar.update(dt);
            }
            //
            base.update(dt);
        }
        public override void draw()
        {
            base.draw();
        }

        public void StartPVP_Game(CCObject sender)
        {


            //ResDef.g_GUIBatchNode.removeChild(pClickToStart, true);
            CircleRunScene.UseAI = false;
            SimpleAudioEngine.sharedEngine().playEffect(ResDef.g_SelectSound);
            Thread.Sleep(400);
            SimpleAudioEngine.sharedEngine().stopBackgroundMusic();
            CCDirector.sharedDirector().replaceScene(CircleRunScene.scene());
        }

        public void StartPVC_Game(CCObject sender)
        {


            //ResDef.g_GUIBatchNode.removeChild(pClickToStart, true);
            CircleRunScene.UseAI = true;
            SimpleAudioEngine.sharedEngine().playEffect(ResDef.g_SelectSound);
            Thread.Sleep(400);
            SimpleAudioEngine.sharedEngine().stopBackgroundMusic();
            CCDirector.sharedDirector().replaceScene(CircleRunScene.scene());
        }
    }
}
